package com.spi.soconversion.jmsqueue;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.SOCFileWriter;
import com.spi.soconversion.util.SOCUtility;

public class JMSFileReader {

	private String fileName;
	private String d_logFileName;
	private SOCFileWriter d_fileWriter;	

	public JMSFileReader(String fileName,String logFileName, SOCFileWriter fileWriter)
	{
		this.fileName = fileName;
		d_logFileName = logFileName;
		d_fileWriter = fileWriter;
	}

	public String fileReader() throws SOCException
	{
		try {
			int len = (int)(new File(fileName).length());
			FileInputStream fis =
				new FileInputStream(fileName);
			byte buf[] = new byte[len];
			fis.read(buf);
			fis.close();
			int cnt = 0;
			for (int i = 0; i < len; i++) {
				if (buf[i] == '\n')
					cnt++;
			}
			return new String(buf);
		}
		catch (IOException ioe) {
			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
					ErrorConstants.FILE_READ_ERROR)+fileName+ioe.toString()));
			throw new SOCException(
					ErrorMessageReader.getError(
							ErrorConstants.FILE_READ_ERROR)+fileName+ioe.toString());
		}
	}

}
